import axios from '../utils/axios'
import router from '../router/index'
import {LOGIN_PATH} from '../config'
import {LOCAL, get} from '../utils/storage'

// 拦截请求加上token
axios.interceptors.request.use(function (config) {
  if (config.url.indexOf(LOGIN_PATH) < 0) {
    const info = get('token', LOCAL)
    if (info) {
      config.headers.Authorization = info.token
    }
  }
  return config
}, function (err) {
  return Promise.reject(err)
})

axios.interceptors.response.use(function (res) {
  switch (res.data.code) {
    case 401:
      router.push(LOGIN_PATH)
      break
    default:
  }
  return res;
})
